package com.eyeem.recyclerviewtools.sample;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.eyeem.recyclerviewtools.sample.adapter.FragmentAdapter;
import com.eyeem.recyclerviewtools.sample.fragment.ViewPagerFragment;
import com.eyeem.recyclerviewtools.sample.refresh.PullRefreshLayout;
import com.eyeem.recyclerviewtools.sample.utils.BlurUtil;
import com.eyeem.recyclerviewtools.sample.widget.AppBarStateChangeListener;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class PullRefreshLayoutActivity extends BaseActivity {
    private final static String TAG = "ViewPager3Activity";
    private LinearLayout head_layout;
    private PullRefreshLayout swipeRefreshLayout;
    private TabLayout toolbar_tab;
    private ViewPager main_vp_container;
    //private NestedScrollView nestedScrollView;
    private CollapsingToolbarLayout mCollapsingToolbarLayout;
    SampleActivity.CollapsingToolbarLayoutState state = SampleActivity.CollapsingToolbarLayoutState.EXPANDED;
    FragmentAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pull_refresh_layout_bak);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

        final AppBarLayout app_bar_layout = (AppBarLayout) findViewById(R.id.app_bar_layout);
        Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
        //nestedScrollView = (NestedScrollView) findViewById(nestedScrollView);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        head_layout = (LinearLayout) findViewById(R.id.head_layout);
        //
        swipeRefreshLayout = (PullRefreshLayout) findViewById(R.id.swipe_container);
        mCollapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout);

        boolean useRxJava = true;
        if (useRxJava) {
            blur();
        } else {
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.bg);
            head_layout.setBackgroundDrawable(new BitmapDrawable(BlurUtil.fastblur(this, bitmap, 180)));
            //mCollapsingToolbarLayout.setContentScrim(new BitmapDrawable(BlurUtil.fastblur(this, bitmap, 180)));
            mCollapsingToolbarLayout.setContentScrimColor(getResources().getColor(R.color.colorPrimary));
        }

        //使用CollapsingToolbarLayout必须把title设置到CollapsingToolbarLayout上，设置到Toolbar上则不会显示
        app_bar_layout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                if (state == State.EXPANDED) {
                    mCollapsingToolbarLayout.setTitle("涩郎");//设置title为EXPANDED
                } else if (state == State.COLLAPSED) {
                    mCollapsingToolbarLayout.setTitle("涩郎");//设置title不显示
                } else {
                    mCollapsingToolbarLayout.setTitle("涩郎");//设置title为INTERNEDIATE
                }
            }

            @Override
            public void onOffsetChanged(int verticalOffset) {
                Log.e(TAG, "verticalOffset=" + verticalOffset);
                swipeRefreshLayout.setEnabled(verticalOffset >= 0);
            }

            @Override
            public void onSnapDirection(Direction direction) {
            }
        });


        Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                String msg = "";
                switch (menuItem.getItemId()) {
                   /* case R.id.webview:
                        msg += "博客跳转";
                        break;
                    case R.id.weibo:
                        msg += "微博跳转";
                        break;*/
                    case R.id.action_settings:
                        msg += "设置";
                        break;
                }

                if (!msg.equals("")) {
                    Toast.makeText(PullRefreshLayoutActivity.this, msg, Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        };

        mToolbar.setOnMenuItemClickListener(onMenuItemClick);
        toolbar_tab = (TabLayout) findViewById(R.id.toolbar_tab);
        main_vp_container = (ViewPager) findViewById(R.id.main_vp_container);

        adapter = new FragmentAdapter(getSupportFragmentManager());
        adapter.addFragment(ViewPagerFragment.newInstance(), ViewPagerFragment.class.getName() + 0);
        adapter.addFragment(ViewPagerFragment.newInstance(), ViewPagerFragment.class.getName() + 1);
        adapter.addFragment(ViewPagerFragment.newInstance(), ViewPagerFragment.class.getName() + 2);
        adapter.addFragment(ViewPagerFragment.newInstance(), ViewPagerFragment.class.getName() + 3);
        adapter.addFragment(ViewPagerFragment.newInstance(), ViewPagerFragment.class.getName() + 4);
        main_vp_container.setAdapter(adapter);
        main_vp_container.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(toolbar_tab));
        mCollapsingToolbarLayout.setExpandedTitleGravity(Gravity.CENTER);
        mCollapsingToolbarLayout.setCollapsedTitleGravity(Gravity.CENTER);
        toolbar_tab.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(main_vp_container));
        swipeRefreshLayout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler(getMainLooper()).postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }, 3000);
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.tab_pin, menu);
        return true;
    }

    private void blur() {
        Observable.create(
                new Observable.OnSubscribe<BitmapDrawable>() {
                    @Override
                    public void call(Subscriber<? super BitmapDrawable> subscriber) {
                        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.bg);
                        BitmapDrawable bitmapDrawable = new BitmapDrawable(BlurUtil.fastblur(PullRefreshLayoutActivity.this, bitmap, 180));
                        subscriber.onNext(bitmapDrawable);
                        subscriber.onCompleted();
                    }
                })
                .subscribeOn(Schedulers.newThread())// 在非UI线程中执行
                .observeOn(AndroidSchedulers.mainThread())// 在UI线程中执行结果
                .subscribe(new Subscriber<BitmapDrawable>() {
                    @Override
                    public void onNext(BitmapDrawable bitmapDrawable) {
                        head_layout.setBackgroundDrawable(bitmapDrawable);
                        //mCollapsingToolbarLayout.setContentScrim(bitmapDrawable);
                        mCollapsingToolbarLayout.setContentScrimColor(getResources().getColor(R.color.colorPrimary));
                    }

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }
                });
    }

    public enum CollapsingToolbarLayoutState {
        EXPANDED, COLLAPSED, INTERNEDIATE;
    }

}
